package com.changgou.seckill.task;

import com.changgou.seckill.dao.SeckillGoodsMapper;
import com.changgou.seckill.pojo.SeckillGoods;
import com.changgou.seckill.pojo.SeckillOrder;
import com.changgou.seckill.pojo.SeckillStatus;
import entity.IdWorker;
import entity.SystemConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author zhengjd
 * @date 2020/5/23 10:21
 */
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Component
public class MultiThreadingCreateOrder {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;

    /**
     * 多线程下单
     */
    @Async
    public void createOrder(){
        /*System.out.println("开始排队下单。。。。。"+Thread.currentThread().getName());
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("下单成功。。。。。。。"+Thread.currentThread().getName());*/


        try {
            //根据秒杀时间和秒杀商品id从Redis中查询秒杀商品
            //从Redis队列中取出封装的秒杀信息
            SeckillStatus seckillStatus = (SeckillStatus) redisTemplate.boundListOps(SystemConstants.SEC_KILL_USER_QUEUE_KEY).rightPop();
            if (seckillStatus !=null) {

                String time = seckillStatus.getTime();
                String username = seckillStatus.getUsername();
                Long id = seckillStatus.getGoodsId();


                SeckillGoods seckillGoods = (SeckillGoods) redisTemplate.boundHashOps(SystemConstants.SEC_KILL_GOODS_PREFIX + time).get(id);
                if (seckillGoods == null || seckillGoods.getStockCount() <= 0) {
                    throw new RuntimeException("秒杀商品已经卖光");
                }
                SeckillOrder seckillOrder = new SeckillOrder();
                //设置秒杀订单
                seckillOrder.setId(idWorker.nextId());
                seckillOrder.setSeckillId(id);
                seckillOrder.setCreateTime(new Date());
                seckillOrder.setMoney(seckillGoods.getCostPrice());
                seckillOrder.setStatus("0");
                seckillOrder.setUserId(username);

                //将秒杀订单存到Redis中
                redisTemplate.boundHashOps(SystemConstants.SEC_KILL_ORDER_KEY).put(username, seckillOrder);

                //秒杀商品库存减一
                seckillGoods.setStockCount(seckillGoods.getStockCount() - 1);

                //再次判断库存是否为0
                if (seckillGoods.getStockCount() <= 0) {
                    //将秒杀商品数据同步到数据库中
                    seckillGoodsMapper.updateByPrimaryKeySelective(seckillGoods);
                    //将Redis中的库存为0的数据删除
                    redisTemplate.boundHashOps(SystemConstants.SEC_KILL_GOODS_PREFIX + time).delete(id);
                } else {
                    //如果有库存，更新秒杀商品的库存
                    redisTemplate.boundHashOps(SystemConstants.SEC_KILL_GOODS_PREFIX + time).put(id, seckillGoods);

                }
                //修改状态
                seckillStatus.setStatus(2);  //秒杀等待支付
                seckillStatus.setOrderId(seckillOrder.getId());
                seckillStatus.setMoney(Float.valueOf(seckillOrder.getMoney()));
                redisTemplate.boundHashOps(SystemConstants.SEC_KILL_USER_STATUS_KEY).put(username,seckillStatus);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
